﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using TaobaoERP.BusinessServices;
using TaobaoERP.BusinessServices.Common;
using TaobaoERP.Core.Common;
using TaobaoERP.Entity;
using TaobaoERP.Core.Controller;

namespace TaobaoERP.Controllers
{
    public class ProductController : BaseController
    {
        SellerCatService service = new SellerCatService();
        public ActionResult ProductAdd()
        {
            // Add action logic here
            ViewData["StuffStatus"] = new SelectList(DictService.ListDictItemByID(new Guid(DictEnum.商品新旧程度)), 
                "ID", "Name");
            ViewData["Type"] = new SelectList(DictService.ListDictItemByID(new Guid(DictEnum.商品销售类型)), 
                "ID", "Name");
            ViewData["PayType"] = new SelectList(DictService.ListDictItemByID(new Guid(DictEnum.谁承担运费)), 
                "ID", "Name");
            ViewData["SellerCatID"] = new SelectList(SellerCatService.ListSellerCat(CurrentUser,0),
                "ID", "Name");
            
            return View();
        }
        public ActionResult Index()
        {
            ViewData["SellerCats"] = new SelectList(SellerCatService.ListSellerCat(CurrentUser,0),
                "ID", "Name");
            ViewData["totail"] = ItemService.ListItemByNick(Utils.GetCookies("isp_username"), 10, 0).Count;
            return View();
        }
        public JsonResult ListItemByNick(int? Page)
        {
            int PageIndex = Page ?? 0;
            var list = ItemService.ListItemByNick(Utils.GetCookies("isp_username"), 10, PageIndex);
            return Json(list); ;
        }
        public ActionResult Label()
        {
            if (string.IsNullOrEmpty(Utils.GetCookies("sessionid")))
                Utils.SetCookies("sessionid", Session.SessionID);
            //SellerCatService.UpdateSellerCat(Utils.GetCookies("sessionid"), Utils.GetCookies("isp_username"));
            ViewData["nick"] = CurrentUser;
            //ViewData["total"] = SellerCatService.CountSellerCat(Utils.GetCookies("isp_username"));
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void LabelAdd(int? id,FormCollection form)
        {
            SellerCat item ;
            if (id.HasValue)
            {
                item = service.GetInfo(id.Value);
                UpdateModel(item);
                service.update(item);
            }
            else
            {
                item = new SellerCat();
                item.Name = form["Name"];
                item.Nick = CurrentUser;
                SellerCatService.Add(item);
            }

            Response.Write("<script>self.parent.tb_remove(true);</script>");
            Response.ContentType = "text/html";
            
            
        }
        public ActionResult LabelAdd(int? id)
        {
            if (id.HasValue)
            {
                ViewData["buttonValue"] = "修改分类";
                ViewData["UpdateUrl"] = id.Value + "/";
                SellerCat item = new SellerCat();
                item=service.GetInfo(id.Value);
                return View(item);
            }
            else{
                ViewData["buttonValue"] = "添加分类";
                return View();
            }
        }

        public JsonResult ListLabel(int? Page)
        {
            int PageIndex = Page ?? 0;
            List<SellerCat> list = SellerCatService.ListSellerCat(CurrentUser, PageIndex);
            //ViewData["list"] = list;
            return Json(list);

        }
        public JsonResult CountLabel()
        {
            JsonResult result = new JsonResult();
            result.Data = SellerCatService.CountSellerCat(CurrentUser); ;
            return result;

        }

        public JsonResult IsLabel(int? id,string Name)
        {
            JsonResult result = new JsonResult();
            result.Data = new SellerCatService().IsExistLabel(id,Name);
            return result;
           
        }

        public JsonResult LabelDel(int id)
        {
            
            service.Del(id);
            JsonResult result = new JsonResult();
            result.Data = true;
            return result;
        }

        public JsonResult GetTaobaoLabel()
        {
            SellerCatService.UpdateSellerCat(CurrentUser);
           JsonResult result= new JsonResult();
           result.Data = true;
           return result;
        }
        
    }
}
